/**
 * Copyright (C), 2015, 上海赛可电子商务有限公司
 * Author:   张卫明
 * Date:     2016-4-27 
 * lever:    page
 * Description: 提交评论
 */

(function(){
	
		var imgArr = [],
			imgNum = 5,
			imgnumed = $('#imgnumed');
		
		// 实例化
        var uploader = WebUploader.create({
            pick: {
                id: '#filePicker',
                label: '+',
                multiple:false
            },
            formData: {
            },
            paste: '#uploader',
            auto: true,
            duplicate: true,
            swf: swfUploaderUrl,
            chunked: false,
            chunkSize: 512 * 1024,
            fileSingleSizeLimit: 3*1024*1024,
            server: mallBase + '/member/toupload.htm',
            accept : {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png',
                mimeTypes: 'image/*'
            }
        });
        
        //上传出错时
        uploader.onError = function( code ) {
        
        	var errorText = '';
        	switch(code)
			{
				case 'Q_TYPE_DENIED':
				  errorText = '您上传的文件类型不匹配，请上传gif,jpg,jpeg,png的图片文件';
				  break;
				case 'F_EXCEED_SIZE':
				  errorText = '上传的单个文件大小不能大于3M';
				  break;
				default :
				  errorText = code;
				  break;
			}
        	
			ECar.easyDialog.open({
				 container : {
					 header : '上传出错',
					 content : '<div style="text-align:center">'+ errorText +'</div>'
				 }
			 });
			 
        };
        
        //当文件上传出错时就停止上传
        uploader.onUploadError = function( file , reason) {
        	errorState = true;
        };
        
        
        
        //把上传成功的图片路径填充到隐藏域中
        uploader.onUploadSuccess = function( file , response ) {
        	
        	$('#uploader').prepend('<div class="comment-uploader" style="margin-bottom:10px;"><img src="' + upload_imgurl +response['result']['imgUrl']+'"/><i class="remove-img" imgurl="'+response['result']['imgUrl']+'"></i></div>');
    	 	imgArr.push(response['result']['imgUrl']);
    	 	
    	 	imgNum = imgNum -1;
    	 	imgnumed.html(5-imgNum);
    	 	
    	 	if(imgNum<=0){
    	 		$('#filePicker').hide();
    	 	} else {
    	 		$('#filePicker').show();
    	 	}
    	 	
        };
        
        //删除图片
        $(document).on('click','.remove-img',function(){
        	
        	for(var i=0;i<imgArr.length;i++){
        		if(imgArr[i] == $(this).attr('imgurl')){
        			imgArr.splice(i, 1);
        			$(this).parents('.comment-uploader').remove();
        			imgNum = imgNum +1;
        			imgnumed.html(5-imgNum);
        			
        			if(imgNum>0){
        				$('#filePicker').show();
        			} else {
        				$('#filePicker').hide();
        			}
        			
        		}
        	}
        	
        });
        
        //提交表单
        var starNode = $('.star','.my-comm-commit'),
        	commentCont = $('#comment-cont'),
        	anonymousVal = $('#anonymous'),
        	commentSubmit=$('#comment-submit');
		
		starNode.on('click',function(){
			
			var starIndex = $(this).index(),
				i=0;
			
			if($(this).hasClass('star-solid')){
				starIndex = starIndex-1;
			}
			
			for(i;i<starNode.length;i++){
				if(i<=starIndex){
					starNode.eq(i).attr('class','star star-solid');
				} else {
					starNode.eq(i).attr('class','star star-hollow');
				}
			}
			
		});

        
        commentSubmit.on('click',function(){
        	
        	var starLen = $('.star-solid','.my-comm-commit').length,
        		commentInfo = $.trim(commentCont.val().replace(/\n/g,"@@@@"));
        		
        	if( starLen<=0 ){
        		ECar.easyDialog.open({
					 container : {
						 header : '提交评论',
						 content : '<div style="text-align:center">请选择评分</div>'
					 }
				 });
        		
        		return false;
        		
        	}
        	
        	if( commentInfo == '' || commentInfo == commentCont.attr("placeholder")){
        		ECar.easyDialog.open({
					 container : {
						 header : '提交评论',
						 content : '<div style="text-align:center">请填写评价</div>'
					 }
				 });
        		
        		return false;
        		
        	}
        	
        	var imgLength = imgArr.length;
        	
        	var formdata = {
		        	score: starLen,
		        	content: $.trim(commentInfo),
		        	'evaluationPicVOList[0].picUrl':imgArr[0],
		        	'evaluationPicVOList[1].picUrl':imgArr[1],
		        	'evaluationPicVOList[2].picUrl':imgArr[2],
		        	'evaluationPicVOList[3].picUrl':imgArr[3],
		        	'evaluationPicVOList[4].picUrl':imgArr[4],
		        	iptAnonymity: $('#anonymous').is(':checked'),
		        	objectId: objectId
		        };
			
			$.ajax({
				url:"/member/saveEvaluation.htm",
				dataType: "json",
				cache: false,
				type: 'POST',
				data:formdata,
				contentType: "application/x-www-form-urlencoded; charset=utf-8", 
				success: function(data) {
					if(data){
						doOpenOrder('提交评论','评论提交成功');
					}else{
						doOpenOrder('提交评论','评论提交失败');
					}
				},
				error: function(){
					doOpenOrder('提交评论','评论提交失败');
				}
			});
        	
        });
        
		
	})();


function doOpenOrder(title,msg){
	ECar.easyDialog.open({
		 container : {
			 header : title,
			 content : '<div style="text-align:center">'+msg+'</div>'
		 },
		autoClose:3000,
        callback:function(){
	   		  window.location.href = '/member/mallOrderDetail/'+objectId+'.htm';
        }
	 });
}

